﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using CommunityToolkit;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HeimaSoft.Model;
using HeimaSoft.View;
using PetaPoco;

namespace HeimaSoft.ViewModel
{
    public partial class {tblName}EditViewModel : BaseViewModel
    {

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public {tblName}EditViewModel()
        {

            int id = GetInt("id");
            InitPage(id);
            LoadData();

        }
        #endregion

        #region 业务函数
        /// <summary>
        /// 初始化查询条件
        /// </summary>
        private void InitPage(int id)
        {
            PostData.Id = id;
            IsAdd = id <= 0;
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            if (IsAdd)
            {
                QueryResult.Model = new {tblName}();
            }
            else
            {
                QueryResult.Model = db.FirstOrDefault<{tblName}>("where id=@0", PostData.Id);
            }
        }

        /// <summary>
        /// 按钮事件
        /// </summary>
        [RelayCommand]
        public void BtnSave()
        {
            if (IsAdd)
            {
                var mod = QueryResult.Model;
                #region 数据验证
[loop start]
                if (!HasValue(mod.{fldName})) { LayerMsg("[fldTitle]不能为空"); return; }
[loop end]^
                #endregion
                //mod.Alarm_time = DateTime.Now;
                try
                {
                    db.Insert(mod);
                    LayerAlert("新增成功");
                }
                catch (Exception ex)
                {
                    LayerMsg($"错误：{ex.Message}");
                }
            }
            else
            {
                var mod = QueryResult.Model;
                #region 数据验证
[loop start]
                if (!HasValue(mod.{fldName})) { LayerMsg("[fldTitle]不能为空"); return; }
[loop end]^
                #endregion
                try
                {
                    db.Update(mod);
                    LayerAlert("修改成功");
                }
                catch (Exception ex)
                {
                    LayerMsg($"错误：{ex.Message}");
                }
            }

        }

        #endregion

        #region 实体对象
        /// <summary>
        /// 页面类型
        /// </summary>
        private bool IsAdd = true;
        /// <summary>
        /// 查询条件
        /// </summary>
        [ObservableProperty]
        Cls_post_data postData = new Cls_post_data();

        [ObservableObject]
        public partial class Cls_post_data
        {
            [ObservableProperty]
            public int id = 0;
        }

        /// <summary>
        /// 查询结果
        /// </summary>
        [ObservableProperty]
        Cls_query_result queryResult = new Cls_query_result();

        [ObservableObject]
        public partial class Cls_query_result
        {
            [ObservableProperty]
            public int total_count = 0;
            [ObservableProperty]
            public {tblName} model = new {tblName}();
        }
        #endregion


    }
}





